/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.SkuLang;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.dao.SkuLangMapper;
import com.yami.shop.service.SkuLangService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import com.yami.shop.bean.model.Sku;
import com.yami.shop.dao.SkuMapper;
import com.yami.shop.service.SkuService;

/**
 *
 * @author lgh on 2018/09/29.
 */
@Service
public class SkuServiceImpl extends ServiceImpl<SkuMapper, Sku> implements SkuService {

    @Autowired
    private SkuMapper skuMapper;
	@Autowired
    private SkuLangMapper skuLangMapper;
	@Autowired
    private SkuLangService skuLangService;

	@Override
	@Cacheable(cacheNames = "skuList",  key = "#prodId +':'+#dbLang")
	public List<Sku> listByProdId(Long prodId, Integer dbLang) {
		List<Sku> skuList = skuMapper.listByProdId(prodId);
		if(CollectionUtils.isEmpty(skuList)){
			return null;
		}
		// 将当前语言的prodName给sku
		for (Sku sku : skuList) {
			for (SkuLang skuLang : sku.getSkuLangList()) {
				if(Objects.equals(skuLang.getLang(),dbLang)){
					sku.setProdName(skuLang.getProdName());
				}
			}
		}
		return skuList;
	}

	@Override
	@Cacheable(cacheNames = "sku", key = "#skuId +':'+#lang")
	public Sku getSkuBySkuId(Long skuId, Integer lang) {
		return skuMapper.getSkuBySkuId(skuId,lang);
	}

	@Override
	@Caching(evict = {
			@CacheEvict(cacheNames = "sku", key = "#skuId +':'+#lang"),
			@CacheEvict(cacheNames = "skuList", key = "#prodId+':'+#lang"),
			@CacheEvict(cacheNames = "skuLang", key = "#skuId +':'+#lang")
	})
	public void removeSkuCacheBySkuIdAndLang(Long skuId, Long prodId, Integer lang) {

	}

    @Override
    public void updateSku(Sku sku) {
		skuMapper.updateById(sku);
		if(StrUtil.isNotBlank(sku.getProdNameEn())) {
			skuLangMapper.updateSkuLangById(sku.getSkuId(), LanguageEnum.LANGUAGE_EN.getLang(), sku.getPropertiesEn(), sku.getSkuNameEn(), sku.getProdNameEn());
		}
		skuLangMapper.updateSkuLangById(sku.getSkuId(),LanguageEnum.LANGUAGE_ZH_CN.getLang(),sku.getProperties(),sku.getSkuName(),sku.getProdNameCn());
    }

	@Override
	public void insertBatchAndLang(List<Sku> skuList) {
		skuMapper.insertBatchReturnId(skuList);
		// 保存语言表
		saveSkuLang(skuList);
	}

	@Override
	@Cacheable(cacheNames = "skuLang", key = "#skuId +':'+#dbLang")
	public Sku getSkuAndLangBySkuId(Long skuId, Integer dbLang) {
		Sku sku = skuMapper.getSkuAndLangBySkuId(skuId);
		if(Objects.equals(I18nMessage.getDbLang(),1) && StrUtil.isNotBlank(sku.getProdNameEn())){
			sku.setProdName(sku.getProdNameEn());
			sku.setSkuName(sku.getSkuNameEn());
		}
		return sku;
	}

	private void saveSkuLang(List<Sku> skuList) {
		List<SkuLang> skuLangs = new ArrayList<>();
		for (Sku sku : skuList) {
			SkuLang skuLang = new SkuLang();
			skuLang.setProdName(sku.getProdNameCn());
			skuLang.setSkuId(sku.getSkuId());
			skuLang.setSkuName(sku.getSkuName());
			skuLang.setProperties(sku.getProperties());
			skuLang.setLang(LanguageEnum.LANGUAGE_ZH_CN.getLang());
			skuLangs.add(skuLang);
			if(StrUtil.isNotBlank(sku.getProdNameEn())) {
				SkuLang skuLangEn = new SkuLang();
				skuLangEn.setProdName(sku.getProdNameEn());
				skuLangEn.setSkuName(sku.getSkuNameEn());
				skuLangEn.setProperties(sku.getPropertiesEn());
				skuLangEn.setSkuId(sku.getSkuId());
				skuLangEn.setLang(LanguageEnum.LANGUAGE_EN.getLang());
				skuLangs.add(skuLangEn);
			}
		}
		skuLangService.saveBatch(skuLangs);
	}
}
